//
//  UIView+CustomLayer.m
//  TuTin
//
//  Created by Cocoa on 2020/3/31.
//  Copyright © 2020 Fullen. All rights reserved.
//

#import "UIView+CustomLayer.h"

@implementation UIView (CustomLayer)

/**
 * 用于 view 切圆角 同时 设置四周阴影. layer 加到最底层.
 *
 * shadowRadius: 模糊计算的半径,设置为0的话则为一个矩形块.
 * shadowColor: 模糊阴影颜色 默认为 #586377.
 * shadowOpacity: 阴影透明度,默认为0则看不到阴影.
 * cornerRadius: 切圆角半径，默认为0 不切圆角.
 * backgroundColor: 背景颜色，默认为#FFFFFF.
 */

- (void)setViewLayerShadowRadius:(CGFloat)shadowRadius
                     shadowColor:(UIColor *)shadowColor
                   shadowOpacity:(CGFloat)shadowOpacity
                    cornerRadius:(CGFloat)cornerRadius
                 backgroundColor:(UIColor *)backgroundColor {
    
    
    backgroundColor = backgroundColor ? backgroundColor : [UIColor whiteColor];
    shadowColor = shadowColor ? shadowColor :[UIColor purpleColor];
    shadowRadius = shadowRadius > 0 ? shadowRadius : 5.0f;
    shadowOpacity = shadowOpacity > 0 ? shadowOpacity : 0.2f;
    cornerRadius = cornerRadius > 0 ? cornerRadius : 0.f;
    
    
    CALayer *sublayer =[CALayer layer];
    sublayer.backgroundColor = backgroundColor.CGColor;
    sublayer.shadowOffset = CGSizeMake(0, 0);
    sublayer.shadowRadius = shadowRadius;
    sublayer.shadowColor = shadowColor.CGColor;
    sublayer.shadowOpacity = shadowOpacity;
    sublayer.frame = self.bounds;
    sublayer.cornerRadius = cornerRadius;

    [self.layer insertSublayer:sublayer atIndex:0];
}


/**
 * 简单设置 View 四周阴影
 *
 * shadowRadius: 模糊计算的半径,为0的话则为一个矩形块, 设置默认为5.f .
 * shadowColor: 模糊阴影颜色 设置默认为 #586377.
 * shadowOpacity: 阴影透明度,默认为0则看不到阴影 设置默认为0.2f.
 * shadowOffset: 阴影偏移 默认为 CGSizeMake(0,-3), 设置为 CGSizeMake(0,0), 不作参数
 */
- (void)setViewShadowRadius:(CGFloat)shadowRadius
                     shadowColor:(UIColor *)shadowColor
                   shadowOpacity:(CGFloat)shadowOpacity {
    
    shadowColor = shadowColor ? shadowColor :[UIColor purpleColor];
    shadowRadius = shadowRadius > 0 ? shadowRadius : 5.0f;
    shadowOpacity = shadowOpacity > 0 ? shadowOpacity : 0.2f;
    
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOffset = CGSizeMake(0,0);
    self.layer.shadowOpacity = shadowOpacity;
    self.layer.shadowRadius = 5;
}

- (void)setCornerWithCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii {
    
    CGRect bounds = self.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)setCornerWithCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii bounds:(CGRect)bounds {
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

@end
